<template>
  <view class="container">
    <u-navbar
      :is-fixed="false"
      :border-bottom="false"
      :is-back="true"
      back-icon-name="arrow-leftward"
      title="详情"
      :background="{ background: '#fff' }"
      title-color="#000000"
    >
    </u-navbar>
    <u-calendar :min-date="minDate" :max-date="maxDate" v-model="showC" :mode="mode" @change="change"></u-calendar>
    <u-toast ref="uToast" />
    <view class="content">
      <u-swiper height="400" :list="swipers"></u-swiper>
      <view class="name">{{ teacherInfo.name }}</view>
      <view class="name">擅长：{{ teacherInfo.goodAt }}</view>
      <view class="name">从业时间：{{ teacherInfo.time }}</view>
    </view>
    <view class="comment">
      <view class="title">评论</view>
      <view v-for="c in comments" style="margin-bottom: 20rpx;border-bottom: 1rpx solid #ccc;padding-bottom: 20rpx;">
        <view class="flex-center">
          <image class="avatar" :src="imgUrl + c.author.avatar" mode="aspectFit|aspectFill|widthFix" lazy-load="false" binderror="" bindload=""></image>
          <view class="username">{{c.author.name}} : </view>
        </view>
        <view style="margin: 8rpx 0;">{{c.contents}}</view>
        <view style="color: #666;margin-bottom: 20rpx;font-size: 24rpx;">{{c.aligntime}}</view>
      </view>
    </view>
    <view class="pay" v-if="userInfo.id && userInfo.role !== '咨询师'">
      <view class="cart" @click="openModal">预约</view>
    </view>
  </view>
</template>

<script>
import { addChat, getComment } from "../../api/system/api";
import moment from "moment";
export default {
  data() {
    return {
      userInfo: {},
      good: {},
      swipers: [],
      imgUrl: "http://127.0.0.1:3000/",
      teacherInfo: {},
      content: '',
      showC: false,
      mode: 'date',
      comments: [],
      minDate: moment().format('YYYY-MM-DD'),
      maxDate: moment().add(15, 'days').format('YYYY-MM-DD'),
    };
  },
  onLoad(option) {
    this.teacherInfo = JSON.parse(option.teacher)
    this.swipers = this.teacherInfo.application.map(v => {
      return `${this.imgUrl}${v}`
    })
    this.getComments()
    uni.getStorage({
      key: "userInfo",
      success: (res) => {
        this.userInfo = res.data;
      },
      error: (res) => {
      },
    });
  },
  methods: {
    getComments() {
      getComment({
        teacherId: this.teacherInfo.id,
        page: 1,
        pageSize: 1000
      }).then(res => {
        this.comments = res.data.data
        console.log(this.comments)
      })
    },
    addComment() {
      if (!this.content) {
        this.$refs.uToast.show({
          title: "请输入咨询内容",
          type: "error",
        });
        return
      }
      console.log(this.userInfo)
      console.log(this.teacherInfo)
      console.log(this.content)
      addComment({

      }).then(res => {
        console.log(res)
        if (res.code == 200) {

        } else {
          this.$refs.uToast.show({
            title: res.msg,
            type: "error",
          });
        }
      })

    },
    openModal() {
      if (this.userInfo.id) {
        this.showC = true;
      } else {
        this.$refs.uToast.show({
          title: "请先登录~",
          type: "error",
        });
      }
    },
    change(time) {
      addChat({
        aligntime: time.result,
        contents: '[]',
        status: '0',
        userId: this.userInfo.id,
        teacherId: this.teacherInfo.id,
      }).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$refs.uToast.show({
            title: "预约成功~",
            type: "success",
          });
        }
      })
    }
  },
};
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  overflow: hidden;
  background: #fff3f3;
}
.cover {
  width: 160rpx;
  height: 180rpx;
}
.each-good {
  width: 100%;
  height: 200rpx;
  background: #fff;
  padding: 12rpx 20rpx;
  display: flex;
  margin-top: 20rpx;
  justify-content: space-between;

  .info {
    width: calc(100% - 200rpx);
    position: relative;
    .icon {
      position: absolute;
      right: 20rpx;
      top: 30%;
    }
  }
  .title {
    font-size: 30rpx;
    font-weight: bold;
    margin-bottom: 10rpx;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    overflow: hidden;
  }
  .price {
    color: red;
    display: flex;
    margin-bottom: 10rpx;
  }
  .desc {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
    font-size: 22rpx;
  }
}
.name {
  width: 96%;
  margin: 10rpx 2%;
  font-weight: bold;
  font-size: 30rpx;
}
.pay {
	width: 100%;
	position: fixed;
	bottom: 0;
	height: 100rpx;
	padding: 12rpx 20rpx;
	display: flex;
	background: #fff;
  padding-top: 20rpx;
  justify-content: flex-end;
  .cart {
    padding: 10rpx 26rpx;
    height: 60rpx;
    line-height: 40rpx;
    text-align: center;
    color: #fff;
    background: red;
    border-radius: 30rpx;
  }
}
.content {
  padding: 20rpx;
}
.popup{
  padding: 40rpx 30rpx;
}
.full-btn {
  width: 96%;
  margin: 30rpx 2%;
  background: red;
  border-radius: 30rpx;
  text-align: center;
  color: #fff;
  padding: 20rpx 40rpx;
}
.flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.tc {
  width: 100%;
  text-align: center;
}
.comment{
  padding: 30rpx;
  height: 45vh;
  overflow: hidden;
  overflow-y: scroll;
}
.title {
  width: 100%;
  font-size: 30rpx;
  margin-bottom: 20rpx;
}
.avatar {
  width: 50rpx;
  height: 50rpx;
  border-radius: 50%;
  margin-right: 10rpx;
}
.username{
  font-weight: bold;
  font-size: 30rpx;
}
.each-good {
  width: 100%;
  height: 200rpx;
  background: #fff;
  padding: 12rpx 20rpx;
  display: flex;
  margin-top: 20rpx;
  justify-content: space-between;
  position: relative;

  .info {
    width: calc(100% - 200rpx);
    .icon {
      position: absolute;
      right: 30rpx;
      top: 30%;
      z-index: 99;
    }
  }
  .title {
    font-size: 30rpx;
    font-weight: bold;
    margin-bottom: 10rpx;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    overflow: hidden;
  }
  .price {
    color: red;
    display: flex;
    margin-bottom: 10rpx;
  }
  .desc {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
    font-size: 22rpx;
  }
}
.cover {
  width: 160rpx;
  height: 180rpx;
}
.goods {
  padding: 20rpx;
  background: #fff;
}
</style>
